# 编写图片表业务
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from apps.goods.models import SKUImage, SKU
from apps.meiduo_admin.utils import NumPage
from apps.meiduo_admin.serializers.images import SKUImageSerialzier


class ImagesView(ModelViewSet):
    # 指定序列化器
    serializer_class = SKUImageSerialzier
    # 指定查询集
    queryset = SKUImage.objects.all()
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页
    pagination_class = NumPage

    def simple(self, request):
        """
            获取简单sku数据
             "id": "sku表id",
            "name": "sku名称",
        """
        # 查询spu表
        skus = SKU.objects.all()
        # 构造spu数据进行返回
        sku_list = []
        for sku in skus:
            sku_list.append({
                'id': sku.id,
                'name': sku.name
            })
        return Response(sku_list)
